<template>
  <div class="address_edit">
    <van-sticky>
      <van-nav-bar title="编辑收货地址" left-arrow @click-left="back" />
    </van-sticky>
    <van-cell-group>
      <van-field v-model="form.realName" label="收件人" placeholder="收件人" />
      <van-field v-model="form.cellPhone" label="手机号码" placeholder="手机号码" />
      <van-field v-model="form.area" label="所在地区" placeholder="所在地区" />
      <van-field v-model="form.address" label="详细地址" placeholder="详细地址" />
      <van-field v-model="form.email" label="邮箱" placeholder="邮箱" />
    </van-cell-group>
    <div class="default row-between-center">
      <label for="">设为默认地址</label>
      <van-switch v-model="form.deft" active-value="1" inactive-value="0" size="20" />
    </div>
    <div class="btns row-between-center">
      <div class="btn btn_white" @click="delInvoice">删除</div>
      <div class="btn" @click="editInvoice">编辑</div>
    </div>
  </div>
</template>

<script> 
import api from 'api/api'
import { mapState, mapMutations } from 'vuex'
export default {
  data () {
    return {
      chosenAddressId: '1',
      form: {
        realName: '',
        cellPhone: '',
        area: '',
        address: '',
        email: '',
        deft: 0
      },
    }
  },
  computed: {
    ...mapState(['address'])
  },
  created () {
    this.getDetail()
  },
  methods: {
    getDetail () {
      var def = this.address.filter((res) => {
        return res.id == this.$route.params.id
      })
      console.log(def)
      this.form = def[0]
    },
    editInvoice () {
      api.addressUpdate({
        data: this.form
      }).then(res => {
        this.$toast('编辑成功')
        this.back()
      })
    },
    delInvoice () {
      api.addressDel({
        data: {
          id: this.$route.params.id
        }
      }).then(res => {
        this.$toast('删除成功')
        this.back()
      })
    },
    back () {
      window.history.go(-1)
    }
  }
}
</script>

<style lang="scss">
.address_edit {
  min-height: 100vh;
  // background-color: #f5f5f5;
  text-align: left;
  .btns {
    padding: 0.3rem;
    margin-top: 0.6rem;
  }
  .btn {
    width: 3.2rem;
    line-height: 0.76rem;

    text-align: center;
    background: linear-gradient(
      96deg,
      rgba(72, 143, 240, 1),
      rgba(51, 130, 239, 1)
    );
    border-radius: 0.45rem;
    margin: 0 auto;
    color: #fff;
    font-size: 0.36rem;
    &.btn_white {
      background: rgba(242, 242, 242, 1);
      color: #666;
      font-size: 0.36rem;
    }
  }

  .mb_18 {
    margin-bottom: 0.18rem;
  }
  .mb_105 {
    margin-bottom: 1.05rem;
  }
  .default {
    font-size: 0.4rem;
    color: #999;
    padding: 0.3rem;
    label {
      font-size: 0.3rem;
    }
  }
}
</style>